<?php

class DatacardController extends BaseClass
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $title;	//Page Title
	public $layout = 'main';
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions' => array('index', 'view', 'add', 'delete', 'process', 'admin', 'update', 'UpdateDates'),
				'users' => $this->getAdmins(),
			),
			array('allow',  // allow all users to perform 'index' and 'view' actions
					'actions' => array('index', 'view', 'add', 'delete', 'process', 'admin', 'update', 'UpdateDates'),
					'users' => $this->getSubAdmins(),
			),
		);
	}
	

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$model = new Cms('datacard');
		$model->unsetAttributes();
		$this->title = Yii::app()->name."-Datacard";
		$dates = DatacardsDates::model()->find();
		 if(isset($_GET['Cms'])){
			$model->attributes=$_GET['Cms'];
		 }
		
		$this->render('index',array(
			'model'=>$model,
			'dates'=>$dates,
		));
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->title = Yii::app()->name."-view datacard";
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionAdd()
	{
		$model = new Cms('datacard');
		$this->title = Yii::app()->name."-Add Datacard";
		if(isset($_POST['Cms'])){
			$model->attributes = $_POST['Cms'];
			$model->created = date('Y-m-d H:i:s');
			$model->content = $_POST['Cms']['content'];
			$model->is_datacard = '1';
	
			if($model->validate()){
				if($model->save(false)){
					Yii::app()->user->setFlash('success', 'Datacard successfully created');
					$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard');
				} else{
					Yii::app()->user->setFlash('error', 'Oops, Something bad Happened');
					$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard/add');
				}
			} else{
				Yii::app()->user->setFlash('error', 'Oops, Something bad Happened');
				$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard/add');
			}
		}
		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$this->title = Yii::app()->name." - Update Datacard";
		//echo $_REQUEST['id'];die;
		if( isset($_REQUEST['id']) && !empty($_REQUEST['id']) ){
			$model=$this->loadModel($_REQUEST['id']);
		}
		if(isset($_POST['Cms'])){
			$model->attributes = $_POST['Cms'];
			$model->content = $_POST['Cms']['content'];	
			$model->modified = date('Y-m-d H:i:s');
			
			 if($model->validate()){
				if($model->save(false)){
					Yii::app()->user->setFlash('success', 'Datacard successfully Updated');
					$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard');
				}
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}
	
	/**
	 * update last updated dates and next update dates in datacards
	 */
	
	public function actionUpdateDates(){
		if ( isset($_POST['DatacardsDates']) && !empty($_POST['DatacardsDates'])){
			$model = DatacardsDates::model()->findByPk('1');
			$model->attributes = $_POST['DatacardsDates'];
			$model->created = date('Y-m-d H:i:s');
			$model->update();
			
			Yii::app()->user->setFlash('success','Dates Successfully Updated.');
			$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard');
		}
	}
	
	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	public function actionProcess(){
		$model = new Cms('datacard');
		if($_POST['pageAction'] == 'delete')
		{
			
			if(Cms::model()->deleteByPk($_POST['contact'])) {
				Yii::app()->user->setFlash('success','Record (s) Successfully Deleted');
				$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard');
			}
			else {
				Yii::app()->user->setFlash('error','Record (s) not Deleted');
				$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard');
			}
		}
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Cms the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Cms::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Cms $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cms-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
